package com.cy.model;

import com.alibaba.fastjson.JSONObject;
import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@TableBind(tableName="shift",pkName = "id")
public class Shift extends BasicModel<Shift> {
    public static final Shift dao = new Shift();
    public static final String SELECT_ALL = "`id`,`batch_id`,`production_date`,`status`,`remark`,`fault_num`,`collected_num`,`produced_num`,`put_num`,`opener_id`,`reporter_id`";

    @Override
    public Page<Shift> paginate(Integer pageNumber, Integer pageSize, String select, Shift model) {
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("FROM (shift s LEFT JOIN batch b ON s.batch_id = b.id) ");
        sqlExceptSelectBuffer.append("LEFT JOIN shift_serial_num sn ON s.id = sn.shift_id ");
        StringBuffer selectBuffer = new StringBuffer("SELECT ").append(select);

        pageNumber = pageNumber == null || pageNumber == 0 ? 1 : ++pageNumber;
        pageSize = pageSize == null || pageSize == 0 ? 20 : pageSize;

        if (model != null) {
            int i = 0;
            String[] attrNames = model.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = model.get(attrName);
                if (attrValue != null) {
                    sqlExceptSelectBuffer.append(i++ == 0 ? " WHERE " : " AND ");
                    if(attrName.equals("start_date")){
                        sqlExceptSelectBuffer.append("s.production_date >= ? ");
                    } else if(attrName.equals("end_date")){
                        sqlExceptSelectBuffer.append("s.production_date <= ? ");
                    } else {
                        sqlExceptSelectBuffer.append(" `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
        }
        sqlExceptSelectBuffer.append(" GROUP BY s.id");
        return paras.size() > 0 ? paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray())
                                : paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }

    /**
     * 状态
     */
    public enum Status{
        ENABLED(1, "有效"),
        PAUSE(2, "暂停"),
        DISABLED(0, "无效");

        private int value;
        private String name;

        Status(int value, String name) {
            this.value = value;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public int getValue() {
            return value;
        }

        public static JSONObject toJson(){
            JSONObject jsonObject = new JSONObject(values().length);
            for (Status item : values())
                jsonObject.put(String.valueOf(item.getValue()), item.getName());
            return jsonObject;
        }
    }

    public Shift setId(Long id){
        return this.set("id", id);
    }

    public Long getId(){
        return this.get("id");
    }
        
    public Shift setBatchId(Long batchId){
        return this.set("batch_id", batchId);
    }

    public Long getBatchId(){
        return this.get("batch_id");
    }

    public Shift setOpenerId(Long openerId){
        return this.set("opener_id", openerId);
    }

    public Long getOpenerId(){
        return this.get("opener_id");
    }

    public Shift setReporterId(Long reporterId){
        return this.set("reporter_id", reporterId);
    }

    public Long getReporterId(){
        return this.get("reporter_id");
    }

    public Batch getBatch(){
        return Batch.dao.findById(getBatchId());
    }
        
    public Shift setProductionDate(Date productionDate){
        return this.set("production_date", productionDate);
    }

    public Date getProductionDate(){
        return this.get("production_date");
    }
        
    public Shift setStatus(Integer status){
        return this.set("status", status);
    }

    public Integer getStatus(){
        return this.get("status");
    }
        
    public Shift setRemark(String remark){
        return this.set("remark", remark);
    }

    public String getRemark(){
        return this.get("remark");
    }
        
    public Shift setFaultNum(Long faultNum){
        return this.set("fault_num", faultNum);
    }

    public Long getFaultNum(){
        return this.get("fault_num");
    }
        
    public Shift setCollectedNum(Long collectedNum){
        return this.set("collected_num", collectedNum);
    }

    public Long getCollectedNum(){
        return this.get("collected_num");
    }
        
    public Shift setProducedNum(Long producedNum){
        return this.set("produced_num", producedNum);
    }

    public Long getProducedNum(){
        return this.get("produced_num");
    }
        
    public Shift setPutNum(Long putNum){
        return this.set("put_num", putNum);
    }

    public Long getPutNum(){
        return this.get("put_num");
    }
}